﻿using System;
using System.Globalization;
using Microsoft.SharePoint.Utilities;

namespace QASPRibbon.Helpers
{
    internal static class LocalizationHelper
    {
        public static string Localized(this string resourceStr)
        {
            try
            {
                if ((resourceStr == null) || !resourceStr.StartsWith("$Resources:"))
                {
                    return resourceStr;
                }

                var strArray = resourceStr.Split(new[] { ',' });
                if (strArray.Length != 2)
                {
                    return resourceStr;
                }

                var name = strArray[1].Trim();
                if (name.EndsWith(";"))
                {
                    name = name.Substring(0, name.Length - 1);
                }

                var file = strArray[0];
                file = file.Replace("$Resources:", string.Empty).Trim();
                var res = SPUtility.GetLocalizedString("$Resources:" + name, file, (uint)CultureInfo.CurrentUICulture.LCID);
                return res;
            }
            catch (Exception)
            {
                //HACK
            }

            return resourceStr;
        }
    }
}
